/*
 *  Copyright 2010 4th group G.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package edu.upf.database.impl;

import edu.upf.database.XmlDbDataSource;
import java.util.Properties;
import edu.upf.utils.LogSystem;
import edu.upf.exceptions.DBConnectionFailed;
import java.io.IOException;
import org.xmldb.api.base.*;
import org.xmldb.api.DatabaseManager;


/**
 * <p>Models the data source and connection petition for an eXist XMLDB
 * </p>
 * @author     netjois
 */
public final class EXistDataSource implements XmlDbDataSource{

    private String driver;
    private String uri;
    private Class cl;
    private Database database;
    private Collection col;
    private Properties properties;
    private LogSystem log;
    private static XmlDbDataSource source;
    

    private EXistDataSource() {
        log=new LogSystem(EXistDataSource.class);
        try {
            properties = new Properties();
            properties.load(getClass().getResourceAsStream("/DB.properties"));
            driver = properties.getProperty("XmlDbDataSource/driverClassName");
            uri = properties.getProperty("XmlDbDataSource/url");

            cl = Class.forName(driver);
            database = (Database) cl.newInstance();
            DatabaseManager.registerDatabase(database);

        } catch (IOException ex){
            log.addInfoLog(ex.getMessage());
            throw new DBConnectionFailed("Unable not load file properties",ex);
        } catch (ClassNotFoundException ex) {
            log.addInfoLog(ex.getMessage());
            throw new DBConnectionFailed("DB not found",ex);
        } catch (InstantiationException ex) {
            log.addInfoLog(ex.getMessage());
            throw new DBConnectionFailed("Unable to load Database",ex);
        } catch (IllegalAccessException ex) {
            log.addInfoLog(ex.getMessage());
            throw new DBConnectionFailed("Database access denied",ex);
        } catch(XMLDBException ex){
            log.addInfoLog(ex.getMessage());
            throw new DBConnectionFailed("Unable to register the database",ex);
        }
    }

    /**
     * Gets the static instance of the datasource
     * @return
     */
    public static synchronized XmlDbDataSource getInstance() {
        if (source == null) {
            source = new EXistDataSource();
        }
        return source;
    }

    @Override
    public Collection getCollection() throws XMLDBException {

        col = DatabaseManager.getCollection(uri);
        return col;
    }

    @Override
    public Collection getCollection(String username, String password)
            throws XMLDBException {

        col = DatabaseManager.getCollection(uri,username,password);
        return col;
    }

}
